package com.sanyang.pda.ui.random;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sanyang.pda.adapter.SamplingDetailAdapter;
import com.sanyang.pda.bean.HaveTestedBean;
import com.sanyang.pda.bean.SamplingDetailBean;
import com.sanyang.pda.contract.SamplingDetailContract;
import com.sanyang.pda.event.SamplingEvent;
import com.sanyang.pda.presenter.SamplingDetailPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.projectbase.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SamplingDetailFragment extends BaseMvpFragment<SamplingDetailPresenter> implements SamplingDetailContract.View, OnRefreshListener {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    SamplingDetailAdapter adapter;
    @BindView(R.id.smart)
    SmartRefreshLayout smart;
    private String en;

    public SamplingDetailFragment() {
    }

    public SamplingDetailFragment(String en) {
        this.en = en;
    }

    @Override
    public SamplingDetailPresenter getPresenter() {
        return new SamplingDetailPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_list;
    }

    @Override
    public void initView() {
        smart.setEnableRefresh(true);
        smart.setEnableLoadMore(false);
        smart.setOnRefreshListener(this);
        EventBus.getDefault().register(this);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new SamplingDetailAdapter(R.layout.item_sampling_detail, new ArrayList<>());
        rvList.setAdapter(adapter);
    }

    @Override
    public void initLinsenterner() {

    }

    @Subscribe
    public void onEvent(SamplingEvent samplingEvent) {
        presenter.samplingDetail(en);
    }

    @Override
    public void getData() {
        smart.autoRefresh();
    }

    @Override
    public void setList(List<SamplingDetailBean> list) {
        adapter.setNewInstance(list);
        smart.finishRefresh();
    }

    @Override
    public void showMessage(String msg) {
        super.showMessage(msg);
        smart.finishRefresh();
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        presenter.samplingDetail(en);
    }
}
